<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
            <el-form-item label="轮播图名称" prop="imgName">
                <el-input v-model="queryParams.imgName" placeholder="请输入图片名称" clearable style="width: 240px"
                    @keyup.enter.native="handleQuery" />
            </el-form-item>


            <!-- <el-form-item label="创建时间">
                <el-date-picker v-model="searchTime" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                    range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期">
                </el-date-picker>
            </el-form-item> -->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>


        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['mini:swiper:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['mini:swiper:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                    v-hasPermi="['mini:swiper:remove']">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            <el-table v-loading="loading" :data="swiperList" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column label="图片编号" prop="id" width="120" align="center" />
                <el-table-column label="图片地址" prop="url" align="center" />
                <el-table-column label="图片名称" prop="imgName" align="center" />
                <el-table-column prop="url" label="图片预览" align="center">
                    <template slot-scope="scope">
                        <img :src="baseUrl + scope.row.url" width="100" />
                    </template>
                </el-table-column>

                <el-table-column label="操作" align="center" width="240" class-name="small-padding fixed-width">
                    <template slot-scope="scope" v-if="scope.row.id !== null">
                        <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                            v-hasPermi="['mini:swiper:edit']">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                            v-hasPermi="['mini:swiper:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <!-- 新增 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="100px">
                <el-form-item label="图片名称" prop="imgName">
                    <el-input v-model="form.imgName" placeholder="请输入图片名称" />
                </el-form-item>
                <el-form-item label="图片" prop="standardsName">
                    <div style="display: flex;">
                        <el-upload class="avatar-uploader" action="" :show-file-list="false" :http-request="handleUpload">
                            <el-image v-if="form.url" style="width: 100px; height: 100px;background-color: #f2f2f2;"
                                :src="baseUrl + form.url" fit="contain" />
                            <i v-else class="el-icon-plus avatar-uploader-icon" style="font-size: 30px;"></i>
                        </el-upload>
                        <span v-show="form.url" @click=handleSwiperImgRemove()>
                            <span class="el-upload-list__item-preview">
                                <i class="el-icon-circle-close" style="font-size: 20px;"></i>
                            </span>
                        </span>
                    </div>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitDataScope">确 定</el-button>
                <el-button @click="cancelDataScope">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 修改 -->
    </div>
</template>
<script>
import { listSwiper, addSwiper, updateSwiper, queryById, deleteById } from "@/api/miniprogram/index";
import { uploadImgToCloud, uploadImgPre } from "@/api/menu";

/**
 * 获取token后续上传文件使用 
 */
import { getToken } from "@/utils/auth";
export default {
    name: "swiper",
    components: {},
    data() {
        return {
            baseUrl: process.env.VUE_APP_IMG_BASE_URL,
            multiple: false,
            searchTime: [],
            loading: true,
            ids: [],
            single: true,
            limit: 1,
            showSearch: true,
            total: 0,
            swiperList: [],
            title: "",
            // 发送后台查询数据
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                imgName: "",
                searchBeginTime: undefined,
                searchEndTime: undefined,
            },
            //表单参数
            form: {},

            //增加、删除弹出条件
            open: false,

            //表单验证
            rules: {
                imgName: { require: true, message: "图片名称不能为空", trigger: "blur" }
            }

        };
    },
    created() {
        this.getList();
    },
    methods: {
        getList(params) {
            this.loading = true;
            listSwiper(params).then(res => {
                console.log(res)
                this.swiperList = res.rows
                this.total = res.total
                this.loading = false
            })
        },
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        handleQuery() {
            console.log(this.queryParams);
            this.queryParams.pageNum = 1;
            this.queryParams.searchBeginTime = this.searchTime[0]
            this.queryParams.searchEndTime = this.searchTime[1]
            this.getList(this.queryParams);
        },
        handleAdd() {
            this.open = true;
        },
        handleUpdate(row) {
            console.log(row);
            this.reset();
            const id = row.id || this.ids
            queryById(id).then(res => {
                console.log(res)
                this.form = res.data
                this.open = true
            })

        },
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除轮播图编号为"' + ids + '"的数据项？').then(function () {
                return deleteById(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
            // this.$modal.confirm("是否删除图片" + row.imgName + "?").then(() => {
            //     deleteById(row.id).then(res => {
            //         this.$modal.msgSuccess("删除成功");
            //         this.$tab.refreshPage();
            //     })
            // })
        },
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        submitDataScope() {
            let flag = true;
            if (this.form.id) {
                updateSwiper(this.form).then(res => {
                    if (res.code = 200) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                })
            } else {
                addSwiper(this.form).then(res => {
                    if (res.code = 200) {
                        flag = true;
                    } else {
                        flag = false;
                    }
                })
            }
            if (flag) {
                this.reset();
                this.open = false;
                this.$tab.refreshPage();
                this.$modal.msgSuccess("操作成功")
            } else {
                this.$modal.msgError("操作失败")
            }
        },
        cancelDataScope() {
            this.reset();
            this.fileList = []
            this.open = false;
        },
        reset() {
            this.form = {
                id: undefined,
                imgName: undefined,
                url: undefined
            }
        },
        handleUpload(httpParams) {
            this.loading = true;
            uploadImgPre(httpParams.file.name, "indexSwiperImg").then(res => {
                uploadImgToCloud(res.data, httpParams.file).then(result => {
                    console.log(result);
                    this.$set(this.form, "url", "/" + res.data.key);
                    this.loading = false;
                    this.$modal.msgSuccess("上传成功");
                })
            })
        },
        handleSwiperImgRemove() {
            this.form.url = "";
        },
    }
}

</script>